﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace IOtext
{
    class Program
    {
        static void Main(string[] args)
        {
                /*
                 * Driveinfo：获取计算机驱动器信息
                 */
                //获取所有驱动器
                DriveInfo[] driveInfo = DriveInfo.GetDrives();
                foreach(DriveInfo drive in driveInfo)
                {
                     Console.WriteLine(drive);
                }


                DriveInfo dr = new DriveInfo("D");
                Console.WriteLine("驱动器的名称：" + dr.Name);
                Console.WriteLine("驱动器类型：" + dr.DriveType);
                Console.WriteLine("驱动器的文件格式：" + dr.DriveFormat);
                Console.WriteLine("驱动器中可用空间大小：" + dr.TotalFreeSpace);
                Console.WriteLine("驱动器总大小：" + dr.TotalSize);




                /*
                * Directoryinfo类：文件夹操作
                */
                //创建文件夹
                DirectoryInfo directoryInfo = new DirectoryInfo("D:\\code");
                directoryInfo.Create();
                directoryInfo.CreateSubdirectory("code-1");
                directoryInfo.Delete(true);//删除code

                //查看code中文件夹
                //IEnumerable<DirectoryInfo> dir = directoryInfo.EnumerateDirectories();
                //foreach(var v in dir)
                //{
                //    Console.WriteLine(v.Name);
                //}


                //Directory类：文件夹操作
                //判断文件夹是否存在
                bool res = Directory.Exists("D:\\text");
                if (res)
                {
                    Console.WriteLine("存在");
                }
                else
                {
                    Console.WriteLine("不存在");
                }
                //创建文件夹

                Directory.CreateDirectory("D:\\text\\text1");
                FileInfo fileInfo = new FileInfo("D:\\text\\text1\\text1.txt");
                if (!fileInfo.Exists)
                {
                    //创建文件
                    fileInfo.Create().Close();
                }





                /*
                 * FileInfo类：文件操作
                 */
                fileInfo.Attributes = FileAttributes.Normal;//设置文件属性
                Console.WriteLine("文件路径：" + fileInfo.Directory);
                Console.WriteLine("文件名称：" + fileInfo.Name);
                Console.WriteLine("文件是否只读：" + fileInfo.IsReadOnly);
                Console.WriteLine("文件大小：" + fileInfo.Length);

                //将文件移动到text-1文件夹下
                Directory.CreateDirectory("D:\\text-1");
                //判断目标文件夹中是否含有文件test1.txt
                FileInfo newFileInfo = new FileInfo("D:\\text-1\\test1.txt");
                if (!newFileInfo.Exists)
                {
                    //移动文件到指定路径
                    fileInfo.MoveTo("D:\\text-1\\test1.txt");
                }
                else
                {
                    Console.WriteLine("存在");
                }


            /*
             File类：文件操作
            */

            string path = "D:\\text-1\\test1.txt";
            //文件创造时间
            Console.WriteLine(File.GetCreationTime(path));
            //访问时间
            Console.WriteLine(File .GetLastAccessTime(path));
            //指定时间
            File.SetLastAccessTime(path, new DateTime(2021 , 6 , 18));
            DateTime dt = File.GetLastAccessTime(path);
            Console.WriteLine(dt);

            FileStream fs = File.Create(path);

            fs.Close();//关闭文件流
                      
            string newPath = "D:\\code-1\\test1.txt"; //设置目标路径

            Console.WriteLine(newPath);

            //File.Delete(newPath);




            /*
             * Path类：文件路径操作
             */

            Console.WriteLine("请输入一个文件路径：");
            string pa= Console.ReadLine();

            Console.WriteLine("文件路径：" + Path.GetDirectoryName(path));
            Console.WriteLine("文件扩展名：" + Path.GetExtension(path));

            //更改文件扩展名
            string newPa = Path.ChangeExtension(pa, "doc");
            Console.WriteLine("更改后的文件全名：" + Path.GetFileName(newPa));



            StreamWriter streamWriter = new StreamWriter(path);
            //向文件中写入姓名
            streamWriter.WriteLine("小张");
            //向文件中写入手机号
            streamWriter.WriteLine("19859722865");
            //刷新缓存
            streamWriter.Flush();
            //关闭流
            streamWriter.Close();


            
        }

    }
    }
